/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.merak.core.persistence.jdbc.systems;

import org.merak.core.model.SettingException;
import org.merak.core.persistence.jdbc.DatabaseSystem;
import org.merak.core.persistence.jdbc.settings.ConnectionDetail;

public class Hsqldb extends DatabaseSystem {

	//~ Attributes ///////////////////////////////////////////////////////////////
	//****************************************************************************
	private final String  standardDriver = "org.hsqldb.jdbcDriver";
	private final Integer standardPort   = null;

	//~ Methods //////////////////////////////////////////////////////////////////
	//****************************************************************************
	@Override
	public String getStandardDriver() {
		return this.standardDriver;
	}
	//****************************************************************************
	@Override
	public Integer getStandardPort() {
		return this.standardPort;
	}
	//****************************************************************************
	@Override
	public String formatUrl(ConnectionDetail s) throws Exception {

		// Auxiliary
		StringBuffer url = new StringBuffer("jdbc:hsqldb:");

		// Append subprotocol
		if (s.getDriverType() == null) s.setDriverType("file");
		url.append(s.getDriverType()).append(':');

		// Append Host&Port if remote connection
		if ("hsql".equals(s.getDriverType())) {
			// Validate
			if (s.getHost() == null) throw new SettingException(
				"Database Host is missing"
			);
			// jdbc:hsqldb:hsql://host/[databaseName]
			url.append("//").append(s.getHost()).append(
					(s.getPort() != null) ? ':' + s.getPort() : ""
			);
		}

		// Append database
		url.append('/').append(s.getDatabase());
		return url.toString();

	}
	//****************************************************************************

}

